using System;
using System.Collections.Generic;
using System.Text;
using Acms.Bll;

namespace Acms.DataControls
{
    [System.ComponentModel.Designer(typeof(UserPostUploadDataSourceDesigner))]
    [System.Web.UI.ToolboxData("<{0}:UserPostUploadDataSource runat=server></{0}:UserPostUploadDataSource>")]
    public class UserPostUploadDataSource : GenericDataSource<Bll.UserPostUpload>
    {
        public override bool CanDelete
        {
            get
            {
                return true;
            }
        }

        public override bool CanUpdate
        {
            get
            {
                return true;
            }
        }

        public override bool CanInsert
        {
            get
            {
                return true;
            }
        }

        protected override UserPostUpload PopulateData(UpdateAction action, System.Collections.IDictionary keys, System.Collections.IDictionary values, System.Collections.IDictionary oldValues)
        {
            UserPostUpload tmp = null;
            object concurencyId = null;

            if (action == UpdateAction.Insert)
            {
                int userId = GetValue<int>("UserId", values);
                int postId = GetValue<int>("PostId", values);
                string ipAddress = GetValue<string>("IPAddress", values);

                tmp = UserPostUpload.CreateInstance(userId, postId, System.Net.IPAddress.Parse(ipAddress));
            }
            else
            {
                int id = GetValue<int>("Id", keys);
                concurencyId = GetValue<object>("ConcurencyId", keys);

                tmp = UserPostUpload.GetById(id);
            }

            if (tmp != null)
            {
                tmp.CheckConcurent(concurencyId);

                if (RequiresUpdate<string>("Title", values, oldValues))
                {
                    tmp.Title = GetValue<string>("Title", values);
                }

                if (RequiresUpdate<string>("Comment", values, oldValues))
                {
                    tmp.Comment = GetValue<string>("Comment", values);
                }
            }
            return tmp;
        }

        protected override int SaveChanges(UserPostUpload item)
        {
            item.Update();
            return 1;
        }

        protected override int Delete(UserPostUpload item)
        {
            item.Delete();
            return 1;
        }
    }

    public class UserPostUploadDataSourceDesigner : GenericDataSourceDesigner<Bll.UserPostUpload>
    {

        protected override Acms.Bll.UserPostUpload GetDesignTimeItem(int index)
        {
            Random rnd = new Random();
            Bll.UserPostUpload tmp = Bll.UserPostUpload.CreateInstance(rnd.Next(1, 100), rnd.Next(1, 100), System.Net.IPAddress.Parse("127.1.1.0"));
            return tmp;
        }
    }
}
